package com.leetcode.problems066;

/**
 * https://leetcode-cn.com/problems/plus-one/
 * @Date 2021/12/09 14:34
 * @Author ghery
 * @Version V1.0
 */
public class Solution {
    public int[] plusOne(int[] digits) {
        int n = digits.length - 1;
        int carry = 1;
        for (int i = n; i >=0 ; i--) {
            if(digits[i] + carry > 9){
                carry = (digits[i] + carry) / 10;
                digits[i] = (digits[i] + carry) % 10;
            }else{
                digits[i] = (digits[i] + carry);
                carry = 0;
                break;
            }
        }
        if(carry != 0){
            int[] res = new int[digits.length + 1];
            res[0] = 1;
            System.arraycopy(digits,0,res,1,n);
            return res;
        }
        return digits;
    }

    public static void main(String[] args) {
        int[] strs = {0,8};
        int[]  res = ((new Solution().plusOne(strs)));
        for (int i = 0; i < res.length; i++) {
            System.out.print(res[i]);
            System.out.print(",");
        }
        System.out.println();
    }
}
